package com.ybwh.springboot2.uidgen.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.xfvape.uid.UidGenerator;
import com.ybwh.springboot2.uidgen.mapper.WorkerNodeMapper;
import com.ybwh.springboot2.uidgen.model.po.WorkerNode;
import com.ybwh.springboot2.uidgen.service.WorkerNodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 百度UID Generator 分布式id生成器服务接口实现类
 * </p>
 *
 * @author Fan Beibei
 * @version V1.0
 * @since 2023-04-15 20:53:45
 */
@Slf4j
@Service
public class WorkerNodeServiceImpl implements WorkerNodeService {
    @Resource
    private WorkerNodeMapper workerNodeMapper;

    @Resource
    private UidGenerator uidGenerator;


    @Override
    public PageInfo<WorkerNode> queryByPage(WorkerNode param, int pageNum, int pageSize) {
        Preconditions.checkArgument(pageNum > 0);
        Preconditions.checkArgument(pageSize > 0);

        Page<WorkerNode> page = workerNodeMapper.selectByPage(param, pageNum, pageSize);

        return new PageInfo<>(page);
    }


//------------以上是生成代码-----------------------------------------------------------------------------------------------------------------

    @Override
    public long genUid() {
        long uid = uidGenerator.getUID();
        // 打印生成当前唯一id所用的元素,返回的是个json字符串
        System.out.println(uidGenerator.parseUID(uid));
        return uid;
    }

}
